package com.fulihui.wgj.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import tk.mybatis.spring.annotation.MapperScan;

import javax.sql.DataSource;

/**
 * mybatis数据源配置
 *
 * @author wang_wx
 * @date 2018-06-27
 */
@Configuration
@EnableTransactionManagement
public class MybatisDbConfig {

    /**
     * 数据源-mysql
     */
    @Configuration
    @MapperScan(basePackages = "com.fulihui.wgj.dal.mybatis.mapper", sqlSessionFactoryRef = "sqlSessionFactoryMysql")
    class MybatisMysqlDbConfig {

        @Autowired
        @Qualifier("mysqlDataSource")
        private DataSource mysqlDataSource;

        @Bean
        public SqlSessionFactory sqlSessionFactoryMysql() throws Exception {
            SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
            factoryBean.setDataSource(mysqlDataSource);
            factoryBean.setMapperLocations((new PathMatchingResourcePatternResolver().getResources("classpath:sqlmap/mybatis/*.xml")));
            org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
            configuration.setMapUnderscoreToCamelCase(true);
            factoryBean.setConfiguration(configuration);
            return factoryBean.getObject();
        }

        @Bean("mysqlTransactionManager")
        public DataSourceTransactionManager mysqlDataSourceTransactionManager() {
            return new DataSourceTransactionManager(mysqlDataSource);
        }

        @Bean
        public SqlSessionTemplate sqlSessionTemplateMysql() throws Exception {
            SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryMysql());
            return template;
        }
    }

    /**
     * 数据源-impala
     */
    @Configuration
    @MapperScan(basePackages = "com.fulihui.wgj.dal.impala.mapper", sqlSessionFactoryRef = "sqlSessionFactoryImpala")
    class MybatisImpalaDbConfig {

        @Autowired
        @Qualifier("impalaDataSource")
        private DataSource impalaDataSource;

        @Bean
        public SqlSessionFactory sqlSessionFactoryImpala() throws Exception {
            SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
            factoryBean.setDataSource(impalaDataSource);
            factoryBean.setMapperLocations((new PathMatchingResourcePatternResolver().getResources("classpath:sqlmap/impala/*.xml")));
            return factoryBean.getObject();
        }

        @Bean("impalaTransactionManager")
        public DataSourceTransactionManager impalaDataSourceTransactionManager() {
            return new DataSourceTransactionManager(impalaDataSource);
        }

        @Bean
        public SqlSessionTemplate sqlSessionTemplateImpala() throws Exception {
            SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryImpala());
            return template;
        }
    }
}
